layui.define(['admin','table','form','layarea','element'],function (exports){
    let $ = layui.$
        ,admin  = layui.admin
        ,view   = layui.view
        ,table = layui.table
        ,form = layui.form
        ,layarea = layui.layarea
        ,element = layui.element

    let model = 'home'
        ,module = ['machine','store']

    const active = {
        machine_form:function (d){
            admin.popup({
                title: '机器编辑'
                ,area: ['550px', '600px']
                ,id: 'home-machine-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_form', d).done(function(){

                        layarea.render({
                            elem: '#city-picker',
                            name: 'name',
                            change: function (res) {
                                //选择结果
                                console.log(res);
                            }
                        });

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/machine/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-home-machine'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },

        machine_child_form:function (d){
            admin.popup({
                title: '机器箱柜'
                ,area: ['600px', '600px']
                ,id: 'home-machine-child-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_child_form', d).done(function(){

                    });
                }
            });
        },

        machine_password:function(d){
            admin.popup({
                title: '修改密码'
                ,area: ['550px', '300px']
                ,id: 'home-machine-password-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_password_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/machine/password'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-home-machine'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },

        store_form:function (d) {
            admin.popup({
                title: '商户编辑'
                ,area: ['500px', '300px']
                ,id: 'home-machine-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[1]+'_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/store/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-home-store'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        }
    }

    table.render({
        elem: '#LAY-'+model+'-'+module[0]
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+model+'/'+module[0]
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'store_name',title: '商户'}
            ,{field: 'city_name',title: '城市'}
            ,{field: 'address',title: '地址'}
            ,{field: 'is_online', width: 120, title: '在线状态',templet:'#table-status'}
            ,{title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+model+'-'+module[0]+')', function(obj){

        switch(obj.event){
            case 'add':
                active.machine_form();
                break;
        };
    });

    table.on('tool(LAY-'+model+'-'+module[0]+')', function(obj){
        var data = obj.data;
        if(obj.event === 'all'){
            layer.confirm('确定打开所有门？', function(index){

                admin.req({
                    url: 'home/machine/open/door/all'
                    ,data:data
                    ,done:function (res) {

                        layer.close(index);
                    }
                });
            });
        } else if(obj.event === 'edit'){
            active.machine_form(data);
        } else if (obj.event === 'password'){

            active.machine_password(data);
        } else if (obj.event === 'child'){

            active.machine_child_form(data);
        }
    });

    table.render({
        elem: '#LAY-'+model+'-'+module[1]
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+model+'/'+module[1]
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name', width:80, title: '名称'}
            ,{field: 'secret',title: '接入密钥'}
            ,{title: '状态',width: 120, templet: '#table-switch'}
            ,{field: 'created_at',title: '创建时间',width: 180}
            ,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+model+'-'+module[1]+')', function(obj){

        switch(obj.event){
            case 'add':
                active.store_form();
                break;
        };
    });

    table.on('tool(LAY-'+model+'-'+module[1]+')', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){

        } else if(obj.event === 'edit'){
            active.store_form(data);
        }
    });

    //监听状态工具栏
    form.on('switch(table-'+model+'-'+module[1]+'-status)', function(obj){
        let data = JSON.parse(decodeURIComponent($(this).data('json')));

        admin.req({
            url: model+'/'+module[1]+'/status'
            ,data: {id:data.id,status:obj.elem.checked ?1:0}
        });
    });

    exports('home',active);
});
